'use strict';
const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const fs = require('fs');
const webpackBase = require('./webpack.base');
const CleanWebpackPlugin = require('clean-webpack-plugin'); // 删除dist 目录
let nodeModules = {};
fs.readdirSync('node_modules')
  .filter(function (x) {
    return ['.bin'].indexOf(x) === -1;
  })
  .forEach(function (mod) {
    nodeModules[mod] = 'commonjs ' + mod;
  });

// 打包node服务端运行的配置
module.exports = merge(webpackBase, {
  mode: "development",
  target: "node",
  output: {
    filename: "[name].js",
    path: path.resolve(__dirname, "../ssr"),
    libraryTarget: 'commonjs2'//libraryTarget值为commonjs2时，打包出来的代码可以在node.js环境中运行
  },
  node: {
    __filename: false,
    __dirname: false
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        exclude: /(node_modules|bower_components)/,
        use: [{
          loader: require.resolve('babel-loader'),
          options: {
            babelrc: false,//不使用babelre文件中的配置
            presets: [
              [
                '@babel/preset-env',
                {
                  useBuiltIns: 'entry',
                  targets: {
                    node: true,
                  },
                },
              ],
              '@babel/react',
            ],
            plugins: [
              '@babel/plugin-proposal-class-properties',
              "@babel/plugin-transform-runtime"
            ],
          },

        }],
      },
      {
        test: /\.(sa|sc|c)ss$/,
        use: [{
          loader: "null-loader"
        }]
      },
      {
        test: /\.(sa|sc|c)ss$/,
        use: [{
          loader: "null-loader"
        }]
      },
      {
        // Test for a polyfill (or any file) and it won't be included in your
        // bundle
        test: path.resolve(__dirname, 'node_modules/library/polyfill.js'),
        use: 'null-loader',
      },
    ]
  },
  plugins: [
    // 删除dist打包目录
    new CleanWebpackPlugin(),

    new webpack.DefinePlugin({
      'process.env': {
        'NODE_ENV': JSON.stringify('development'),
      },
      "__CLIENT__": false
    }),
  ],
  externals: nodeModules
})